﻿#include "sqlthread.h"
#include <QDebug>
#include "macro.h"
#include <QtCharts/QSplineSeries>

SqlThread::SqlThread()
{
    series = new QSplineSeries();
    moveToThread(this);
}

void SqlThread::run()
{
    qDebug() << "sql "<<QThread::currentThreadId()<<endl;
    db = QSqlDatabase::addDatabase("QMYSQL");  //连接的MYSQL的数据库驱动
    db.setHostName("localhost");         //主机名
    db.setPort(3306);                    //端口
    db.setDatabaseName("pcsoft");          //数据库名
    db.setUserName("root");              //用户名
    db.setPassword("");            //密码
    db.open();

    //测试连接

    if(!db.open())
    {
        qDebug()<<"connect to mysql error"<<db.lastError().text();
        return ;
    }
    else
    {
         qDebug()<<"connect to mysql OK";
    }
    exec();
}

void SqlThread::uploadDataInj(const int i, const Injector& inj)
{
    QString str = QString("insert into pcsoft.injector(id, time, set_vol, set_per, sta_vol, sta_bat, error) values('%1', '%2', '%3','%4','%5','%6','%7')")
                .arg(QString::number(i)).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
            .arg(QString::number(inj.setting.volume))
            .arg(QString::number(inj.setting.period))
            .arg(QString::number(inj.status_volremain))
            .arg(QString::number(inj.status_battery))
            .arg(QString::number(inj.error));
    QSqlQuery query;
    query.exec(str);
    if(DEBUG) qDebug() << "injector upload"<<endl;
}

void SqlThread::searchDataInj(const QDate& time)
{
    QDate nextDay = time.addDays(1);
    QString str = QString("select * from pcsoft.injector where"
                          " time > '%1' and time < '%2'")
            .arg(time.toString("yyyy-MM-dd"))
            .arg(nextDay.toString("yyyy-MM-dd"));
    QSqlQuery query;
    query.exec(str);
    QString context;
    while (query.next())
    {
        context += QString ("%1 INJECTOR %2 \n Setting: \n Volume: %3 Period: %4 \n Status: \n Volume Remain: %5 Battery: %6 \n error %7\n\n")
            .arg(query.value(1).toString(),
                 query.value(0).toString(),
                 query.value(2).toString(),
                 query.value(3).toString(),
                 query.value(4).toString(),
                 query.value(5).toString(),
                 query.value(6).toString());
    }
    emit sigSearchDataInj(context, time);

}

void SqlThread::uploadDataDTU(const int i, const int current)
{
    QString str = QString("insert into pcsoft.dtu(id, current, time) "
                          "values('%1', '%2', '%3')")
            .arg(QString::number(i))
            .arg(QString::number(current))
            .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    QSqlQuery query;
    query.exec(str);
    qDebug() << str;
}

void SqlThread::searchDataDTU(const QDateTime& time, const int id)
{
    QDateTime nextHour = time.addSecs(3600);
    QString str = QString("select current, time from pcsoft.dtu where id = '%1'"
                          "and time > '%2' and time < '%3'")
            .arg(QString::number(id-1))
            .arg(time.toString("yyyy-MM-dd hh:00:00"))
            .arg(nextHour.toString("yyyy-MM-dd hh:00:00"));
    QSqlQuery query;
    query.exec(str);
    QVector<QPointF> points;
    uint64_t x = 0;
    while (query.next())
    {

        //qDebug() << query.value(1).toDateTime().toString("hh:mm:ss.zzz");
        points.push_back(QPointF(x, query.value(0).toInt()));
        x++;
    }

    series->replace(points);

    emit sigSearchDataDTU(series);
}

SqlThread::~SqlThread()
{
    db.close();
}
